//
//  XQAccountCancelController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQAccountCancelController: XQBaseController {
    
    private lazy var bgImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "account_cancel_bg")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()

    private lazy var textLb : UILabel = {
        let lb = UILabel()
        lb.text = "The account cannot be restored after cancellation. To ensure the security of your account please confirm that the services related to the account have been properly handled before application and pay attention to the following provisions:"
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 16)
        lb.numberOfLines = 0
        lb.textAlignment = .center
        lb.setLineSpacing(lineSpacing: 10)
        return lb
    }()
    
    private lazy var specialLb : UILabel = {
        let lb = UILabel()
        lb.text = "• All borrowings have been completed"
        lb.textColor = .mainTextColor
        lb.font = UIFont.systemFont(ofSize: 16,weight: .medium)
        return lb
    }()
    
    private lazy var sureBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Account cancellation", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .mainTextColor
        btn.layer.cornerRadius = 24
        btn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var calcelBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Close", for: .normal)
        btn.setTitleColor(.mainTextColor, for: .normal)
        btn.backgroundColor = .white
        btn.layer.cornerRadius = 24
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.mainTextColor.cgColor
        btn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        return btn
    }()
    
    public lazy var radioBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "login_unradio"), for: .normal)
        btn.setImage(UIImage(named: "login_radio"), for: .selected)
        btn.isSelected = false
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var protocolLb : UILabel = {
        let lb = UILabel()
        lb.text = "I have read and agreed to the above."
        lb.textColor = UIColor(hex: "#A3A6A9")
        lb.font = UIFont.systemFont(ofSize: 16)
        return lb
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeNav()
        makeUI()
        makeConstraints()
    }

    private func makeNav() {
        self.navTitle = "Account cancellation"
        self.navTitleColor = .white
    }
    
    private func makeUI() {
        self.view.insertSubview(self.bgImageView, belowSubview: self.navigationBar)
        self.bgImageView.addSubview(self.textLb)
        self.bgImageView.addSubview(self.specialLb)
        self.bgImageView.addSubview(self.sureBtn)
        self.bgImageView.addSubview(self.calcelBtn)
        self.bgImageView.addSubview(self.radioBtn)
        self.bgImageView.addSubview(self.protocolLb)
    }
    
    private func makeConstraints() {
        self.bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.textLb.snp.makeConstraints { make in
            make.top.equalTo(UIDevice.xq_statusHeight() + 132)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        self.specialLb.snp.makeConstraints { make in
            make.top.equalTo(self.textLb.snp.bottom).offset(28)
            make.centerX.equalToSuperview()
        }
        self.sureBtn.snp.makeConstraints { make in
            make.top.equalTo(self.specialLb.snp.bottom).offset(92)
            make.left.equalTo(19)
            make.right.equalTo(-19)
            make.height.equalTo(48)
        }
        self.calcelBtn.snp.makeConstraints { make in
            make.top.equalTo(self.sureBtn.snp.bottom).offset(15)
            make.left.equalTo(19)
            make.right.equalTo(-19)
            make.height.equalTo(48)
        }
        self.radioBtn.snp.makeConstraints { make in
            make.top.equalTo(self.calcelBtn.snp.bottom).offset(29)
            make.left.equalTo(27)
            make.width.height.equalTo(20)
        }
        self.protocolLb.snp.makeConstraints { make in
            make.centerY.equalTo(self.radioBtn)
            make.left.equalTo(self.radioBtn.snp.right).offset(10)
        }
    }
    
    @objc func btnClick() {
        self.radioBtn.isSelected = !self.radioBtn.isSelected
    }
    
    @objc func sureClick() {
        if (!self.radioBtn.isSelected) {
            XQHud.showToast(message:"Please read and agree to the Privacy Policy")
            return
        }
        
        XQHud.showLoading(controller: self)
        
        XQNetworkManager.shared.getRequest(url:"/figures/caucasian") {[weak self] response, error in
            XQHud.hideLoading()
            guard let srongify = self else {return}
            
            if (response?.object == 0) {
                XQCache.saveSessionId(sessionId: "")
                XQNetworkManager.shared.setSessionId(sessionId: "")

                srongify.navigationController?.popViewController(animated: true)
                srongify.navigationController?.popViewController(animated: true)
                
                XQRouteTool.jumpHome()
            }else {
                XQHud.showToast(message: response?.patients ?? "Network Connection Error")
            }
        }
    }
    
    @objc func cancelClick() {
        self.navigationController?.popViewController(animated: true)
    }
    

}
